package com.lang.actions;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.lang.beans.Courier;
import com.lang.beans.Task;
import com.lang.service.CourierService;
import com.lang.util.Config;
import com.opensymphony.xwork2.Action;

/**
 * 邮差Action类
 * 
 * @author Lang
 * 
 */
public class CourierAction implements Action {
	private CourierService courierservice; // 邮差服务类，提供各种逻辑操作
	private List tasks; // 邮递员获取到的任务
	private List couriers;
	private Courier courier;
	private String result;

	public CourierService getCourierservice() {
		return courierservice;
	}

	public void setCourierservice(CourierService courierservice) {
		this.courierservice = courierservice;
	}

	public List getTasks() {
		return tasks;
	}

	public void setTasks(List tasks) {
		this.tasks = tasks;
	}

	public Courier getCourier() {
		return courier;
	}

	public void setCourier(Courier courier) {
		this.courier = courier;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List getCouriers() {
		return couriers;
	}

	public void setCouriers(List couriers) {
		this.couriers = couriers;
	}

	@Override
	public String execute() throws Exception {
		return null;
	}

	/**
	 * 注册邮差
	 * @return
	 */
	public String registCourier() {
		String cname = ServletActionContext.getRequest().getParameter(
				"cname");   //获取ajax的data
		String tel = ServletActionContext.getRequest().getParameter(
				"tel"); 
		String email = ServletActionContext.getRequest().getParameter(
				"email");
		String salary = ServletActionContext.getRequest().getParameter(
				"salary");
		int s=Integer.valueOf(salary);
		String permission = ServletActionContext.getRequest().getParameter(
				"permission"); 
		int p=Integer.valueOf(permission);
		String pwd = ServletActionContext.getRequest().getParameter(
				"pwd");
		Courier courier=new Courier(pwd,tel,cname,email,p,s);
		result=courierservice.registCourier(courier);
		return "result";
	}

	// ------------------------------派件部分--------------------------------------------
	/**
	 * 获取 分派到的任务，但没有登记的派件单
	 * 
	 * @return
	 */
	public String getSendTasks() {
		getCourierInfo();
		tasks = courierservice.getCourierTasks(courier.getId(),
				Config.PARCEL_DEALING, Config.SEND_PARCEL);
		// System.out.println("tasks.lenght = "+tasks.size());
		return "success";
	}

	/**
	 * 获取 完成的派件
	 * 
	 * @return
	 */
	public String getSendSuccessTasks() {
		getCourierInfo();
		tasks = courierservice.getCourierTasks(courier.getId(),
				Config.PARCEL_DEAL_SUCCESS, Config.SEND_PARCEL);
		// System.out.println("orders.lenght = "+tasks.size());
		return "success";
	}

	/**
	 * 获取 异常的派件
	 * 
	 * @return
	 */
	public String getSendAbnormalTasks() {
		getCourierInfo();
		tasks = courierservice.getCourierTasks(courier.getId(),
				Config.PARCEL_DEAL_ABNORMAL, Config.SEND_PARCEL);
		// System.out.println("orders.lenght = "+tasks.size());
		return "success";
	}

	// --------------------------------收件部分--------------------------------------------------
	/**
	 * 获取 分派到的任务，但没有登记的收件单
	 * 
	 * @return
	 */
	public String getReceiverTasks() {
		getCourierInfo();
		tasks = courierservice.getCourierTasks(courier.getId(),
				Config.PARCEL_DEALING, Config.RECEIVE_PARCEL);
		// System.out.println("tasks.lenght = "+tasks.size());
		return "success";
	}

	/**
	 * 获取 完成的收件
	 * 
	 * @return
	 */
	public String getReceiverSuccessTasks() {
		getCourierInfo();
		tasks = courierservice.getCourierTasks(courier.getId(),
				Config.PARCEL_DEAL_SUCCESS, Config.RECEIVE_PARCEL);
		// System.out.println("orders.lenght = "+tasks.size());
		return "success";
	}

	/**
	 * 获取 异常的收件
	 * 
	 * @return
	 */
	public String getReceiverAbnormalTasks() {
		getCourierInfo();
		tasks = courierservice.getCourierTasks(courier.getId(),
				Config.PARCEL_DEAL_ABNORMAL, Config.RECEIVE_PARCEL);
		// System.out.println("orders.lenght = "+tasks.size());
		return "success";
	}

	/**
	 * 从session中获取Courier的基本信息
	 */
	public void getCourierInfo() {
		courier = (Courier) ServletActionContext.getRequest().getSession()
				.getAttribute("courier");
		//System.out.println("id = " + courier.getId());
	}
	/**
	 * 获取全部邮递员信息
	 * @return
	 */
	public String getAllCouriers(){
		couriers=courierservice.getAllCourier();
		return "success";
	}

}
